package com.df.ez_game.server.logic.status_sync;

import com.df.ez_game.business.annotation.EzController;
import com.df.ez_game.server.MsgProtocType;
import com.df.ez_game.server.NetProtocType;
import com.df.ez_game.server.NodeType;
import com.df.ez_game.server.logic.LogicMain;
import com.df.ez_game.server.logic.LogicServerNode;
import com.df.ez_game.server.logic.LogicSettings;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 客户端消息类型：
 *      无返回
 *      有返回
 *      服务器推送
 *
 *  服务器内部消息：（通关RPC方式调用）
 *      单个消息
 *          无返回
 *          有返回
 *      广播消息(广播全部或广播部分)
 *          无返回
 *          有返回
 *
 *  单体模式下，生成的api类中的服务器名可以是服务的名
 *  集群模式下，一般都是一个网关，或不同网关，生成时可以把多个服对外服务名统一，站在客户端角度，这些不同服相当于一个统一的服
 */
@SpringBootApplication
public class StatusSyncMain {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(LogicMain.class);
        Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(EzController.class);
        List<Object> collect = beansWithAnnotation.values().stream().filter(clazz -> clazz.getClass().getName().contains("com.df.ez_game.server.logic.status_sync.controllers"))
                .collect(Collectors.toList());
        //下面可以配置成一个bean
        LogicServerNode logicServerNode = new LogicServerNode();
        LogicSettings settings = logicServerNode.getSettings();

        settings.setNodeType(NodeType.SingletonLogic)
                .setPort(13000)
                .setNetProtoc(NetProtocType.WEBSOCKET.getValue())
                .setMsgProtoc(MsgProtocType.JSON.getValue())
                .setWsPath("/ws")
                .setDebug(true)
                .setDocServer(true)
                .setDocServerPort(7896)
                .getControllerInstance().addAll(collect);
        settings.getPushEnumClass().add(SyncPushMsgEnum.class);
        settings.getExceptionClass().add(SyncExceptions.class);

        settings.getFilters().add(new SyncMsgFilter());
        settings.setSessionLifecycle(new SyncSessionLifecycle());
        settings.getStartFailCb().add(channel -> {
            System.out.println("启动失败！");
        });
        settings.getStartSuccessCb().add(channel -> {
            InetSocketAddress addr = (InetSocketAddress) channel.localAddress();
            System.out.println("启动成功！"+addr.getHostString()+"："+addr.getPort()+" path:"+settings.getWsPath());
        });
        Runtime.getRuntime().addShutdownHook(new Thread(logicServerNode::shutDown));
        logicServerNode.start();
    }
}
